﻿using System;
using Company.BLL;
using Company.BLL.KDT.SXXK;
using Company.BLL.Utils;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;
using Company.BLL.KDT.GC;

namespace Company.Interface.KDT.GC.PCTFormTemplate
{
    public partial class PKBoSungTBForm : BaseForm
    {
        public Company.BLL.KDT.GC.HopDong hopdong = new Company.BLL.KDT.GC.HopDong();
        public Company.BLL.KDT.GC.PhuKienDangKy pkdk = new Company.BLL.KDT.GC.PhuKienDangKy();
        public Company.BLL.KDT.GC.PhuKienCollection pkCollection = new Company.BLL.KDT.GC.PhuKienCollection();

        public bool isAdd = true;
        public PKBoSungTBForm()
        {
            InitializeComponent();
        }
        private void uiButton1_Click(object sender, EventArgs e)
        {
            ThietBiGCBoSungForm f = new ThietBiGCBoSungForm();
            f.pkdk = pkdk;
            f.ShowDialog();
            dgList.Refetch();
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void txtMaHS_Leave(object sender, EventArgs e)
        {
            if (!MaHS.Validate(txtMaHS.Text, 10))
            {
                error.SetIconPadding(txtMaHS, -8);
                error.SetError(txtMaHS, "Mã số HS không hợp lệ.");
            }
            else
            {
                error.SetError(txtMaHS, string.Empty);
            }
        }

        private void PKChiTietMaSPForm_Load(object sender, EventArgs e)
        {
            if (!isAdd)
            {
                txtGhiChu.Text = pkdk.GhiChu;
                txtVBCP.Text = pkdk.VBCP;
                //pkdk.PKCollection = Company.BLL.KDT.GC.PhuKien.SelectCollectionBy_Master_ID(pkdk.ID);

            }
            txtMaDV.Text = GlobalSettings.MA_DON_VI;
            txtTen.Text = GlobalSettings.TEN_DON_VI;
            txtSoTiepNhan.Text = pkdk.SoTiepNhan.ToString();
            txtSoHopDong.Text = hopdong.SoHopDong;
            txtSoPhuKien.Text = pkdk.SoPhuKien.ToString();
            txtNgayKyHD.Text = hopdong.NgayKy.ToShortDateString();
            ccNgayPhuKien.Text = pkdk.NgayPhuKien.ToShortDateString();
            ctrDonViHaiQuan.Ma = hopdong.MaHaiQuan;
            if (pkdk.TrangThaiXuLy == TrangThaiXuLy.CHO_DUYET)
            {
                lblTrangThai.Text = "Chờ duyệt";
            }
            else if (pkdk.TrangThaiXuLy == TrangThaiXuLy.CHUA_KHAI_BAO)
            {
                lblTrangThai.Text = "Chưa khai báo";
            }
            else if (pkdk.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
            {
                lblTrangThai.Text = "Đã duyệt";
                ThemTB1.Enabled = Janus.Windows.UI.InheritableBoolean.False;
            }
            pkCollection = (PhuKienCollection)pkdk.PKCollection.Clone(); ;                  
            dgList.DataSource = pkdk.PKCollection;
        }
        private void LuuThongTin()
        {
            pkdk.VBCP = txtVBCP.Text.Trim();
            pkdk.GhiChu = txtGhiChu.Text.Trim();
            this.Close();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            isAdd = false;
            pkdk.PKCollection = pkCollection;
            this.Close();
        }

        private void dgThietBi_DeletingRecord(object sender, Janus.Windows.GridEX.RowActionCancelEventArgs e)
        {
            if (pkdk.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
            {
                ShowMessage("Phụ kiện đã duyệt chính thức. Không được chỉnh sửa", false);
                e.Cancel = true;
                return;
            }
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa nguyên phụ liệu này không?", true) == "Yes")
                {
                    Company.BLL.KDT.GC.PhuKien pkDelete = new Company.BLL.KDT.GC.PhuKien();
                    pkDelete.ID = (long)Convert.ToInt64(e.Row.Cells[13].Text);
                    if (pkDelete.ID > 0)
                    {
                        pkDelete.Delete();
                    }
                    pkDelete.MaHang = e.Row.Cells[3].Text.Trim();
                    pkCollection.Remove(pkDelete);
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void dgThietBi_LoadingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
        {
            e.Row.Cells[6].Text = this.DonViTinh_GetName(e.Row.Cells[5].Value);
            e.Row.Cells[14].Text = this.NguyenTe_GetName(e.Row.Cells[10].Value);
            e.Row.Cells[15].Text = this.Nuoc_GetName(e.Row.Cells[11].Value);            
    
        }

        private void dgThietBi_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {
            ThietBiGCBoSungForm f = new ThietBiGCBoSungForm();
            f.pkdk = pkdk;
            Company.BLL.KDT.GC.PhuKien pk = new Company.BLL.KDT.GC.PhuKien();
            pk.MaHang = e.Row.Cells[2].Text;
            pk.TenHang = e.Row.Cells[3].Text;
            pk.MaHS = e.Row.Cells[4].Text;
            pk.DVT_ID = e.Row.Cells[5].Text;
            pk.SoLuong = Convert.ToDecimal(e.Row.Cells[7].Text);
            pk.STTHang = (int)Convert.ToDecimal(e.Row.Cells[1].Text);
            pk.ID = (long)Convert.ToDecimal(e.Row.Cells[13].Text);
            pk.DonGia = Convert.ToDecimal(e.Row.Cells[8].Text);
            pk.TriGia = Convert.ToDecimal(e.Row.Cells[9].Text);
            pk.NguyenTe = (e.Row.Cells[10].Text);
            pk.NuocXX_ID = (e.Row.Cells[11].Text);
            pk.TinhTrang = (e.Row.Cells[12].Text);
            f.pk = pk;
            f.ShowDialog();
            dgList.Refetch();
        }
        private void cmMainHDGC_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
        {
            switch (e.Command.Key)
            {
                case "ThemTB": uiButton1_Click(null, null); break;
                case "Dong": btnAdd_Click(null, null); break;
                case "Luu": LuuThongTin(); break;
            }
        }
    }
}